package cn.iocoder.yudao.module.written.exam.controller.app.userwrittenexamrecordsdetail;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.written.exam.controller.admin.userwrittenexamrecordsdetail.vo.UserWrittenExamRecordsDetailPageReqVO;
import cn.iocoder.yudao.module.written.exam.controller.app.userwrittenexamrecordsdetail.vo.AppUserWrittenExamRecordsDetailRespVO;
import cn.iocoder.yudao.module.written.exam.dal.dataobject.userwrittenexamrecordsdetail.UserWrittenExamRecordsDetailDO;
import cn.iocoder.yudao.module.written.exam.service.userwrittenexamrecordsdetail.UserWrittenExamRecordsDetailService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

import java.util.ArrayList;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "APP（有关笔试） - 笔试记录详情")
@RestController
@RequestMapping("/written-exam/user-written-exam-records-detail")
@Validated
public class AppUserWrittenExamRecordsDetailController {

    @Value("${zhima.image-prefix-url:}")
    private String imagePrefixUrl;

    @Resource
    private UserWrittenExamRecordsDetailService userWrittenExamRecordsDetailService;

    @GetMapping("/get")
    @Operation(summary = "获得笔试记录详情")
    public CommonResult<AppUserWrittenExamRecordsDetailRespVO> getUserWrittenExamRecordsDetail(@RequestParam("id") Long id) {
        UserWrittenExamRecordsDetailDO userWrittenExamRecordsDetail = userWrittenExamRecordsDetailService.getUserWrittenExamRecordsDetail(id);
        return success(BeanUtils.toBean(userWrittenExamRecordsDetail, AppUserWrittenExamRecordsDetailRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得笔试记录详情分页")
    public CommonResult<PageResult<AppUserWrittenExamRecordsDetailRespVO>> getUserWrittenExamRecordsDetailPage(@Valid UserWrittenExamRecordsDetailPageReqVO pageReqVO) {
        PageResult<UserWrittenExamRecordsDetailDO> pageResult = userWrittenExamRecordsDetailService.getUserWrittenExamRecordsDetailPage(pageReqVO);
        PageResult<AppUserWrittenExamRecordsDetailRespVO> pageResultResp = new PageResult<>();
        pageResultResp.setTotal(pageResult.getTotal());
        List<AppUserWrittenExamRecordsDetailRespVO> appUserWrittenExamRecordsDetailRespVOList = new ArrayList<>();
        pageResult.getList().forEach(userWrittenExamRecordsDetail -> {
            AppUserWrittenExamRecordsDetailRespVO appUserWrittenExamRecordsDetailRespVO = BeanUtils.toBean(userWrittenExamRecordsDetail, AppUserWrittenExamRecordsDetailRespVO.class);
            appUserWrittenExamRecordsDetailRespVO.setFileIds(CollectionUtils.convertStringToList(userWrittenExamRecordsDetail.getFileIds(), String.class));
            List<String> fileUrlsAddPrefix = new ArrayList<>();
            List<String> fileUrls = CollectionUtils.convertStringToList(userWrittenExamRecordsDetail.getFileUrls(), String.class);
            for (String fileUrl : fileUrls) {
                fileUrlsAddPrefix.add(imagePrefixUrl + fileUrl);
            }
            appUserWrittenExamRecordsDetailRespVO.setFileUrls(fileUrlsAddPrefix);
            appUserWrittenExamRecordsDetailRespVOList.add(appUserWrittenExamRecordsDetailRespVO);
        });
        pageResultResp.setList(appUserWrittenExamRecordsDetailRespVOList);
        return success(pageResultResp);
    }

}